package seeqpod;

public class SearchSeeqResultThread extends Thread {

    private String query;
    private String KEY;
    private int startIndex;
    private SeeqPodTrack track = new SeeqPodTrack();

    public SearchSeeqResultThread(String Query, String key, int StartIndex) {
        query = Query;
        KEY = key;
        startIndex = StartIndex;
    }

    @Override
    public void run() {
        try {
            InOut io = new InOut();
            
            String results = io.readData(io.getURL("http://www.seeqpod.com/api/v0.2/" + KEY + "/music/search/" + query + "/" + startIndex + "/1"));
            String location = results.substring(results.indexOf("<location>") + 10, results.indexOf("</location>"));
            String album;
            if (results.indexOf("<album>") != -1 && results.indexOf("<album>") < results.indexOf("</track>")) {
                album = results.substring(results.indexOf("<album>") + 7, results.indexOf("</album>"));
            } else {
                album = "";
            }

            String Genre;
            if (results.indexOf("<genre>") != -1 && results.indexOf("<genre>") < results.indexOf("</genre>")) {
                Genre = results.substring(results.indexOf("<genre>") + 7, results.indexOf("</genre>"));
            } else {
                Genre = "";
            }
            track.setAll(location, results.substring(results.lastIndexOf("<title>") + 7, results.lastIndexOf("</title>")), results.substring(results.indexOf("<creator>") + 9, results.indexOf("</creator>")), album, Genre, startIndex);
        } catch (StringIndexOutOfBoundsException ex) {
        }
    }

    SeeqPodTrack getTrack() {
        return track;
    }
}
